package com.music.demo.util;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.DESede;

/**
 * @author chenxuan
 * @date 2019-07-22 13:18
 */
public class PassportUtil {
    private static String siteId = "{8da9457d-97bb-4255-b9db-4c0e6d593bb7}";
    private static String appUrl = "http://localhost:8000/passport";

    public static String getAuthenticator(String combStr) {
        byte[] sha1 =  DigestUtil.sha1(combStr);

        String sKey = "22362E7A9285DD53A0BBC2932F9733C505DC04EDBFE00D70";
        String sIV = "1E7FA9231E7FA923";
        byte[] bkey = HexUtil.decodeHex(sKey);
        byte[] bIV = HexUtil.decodeHex(sIV);

        String content = combStr + Base64.encode(sha1);
        DESede des = new DESede(Mode.CBC, Padding.PKCS5Padding, bkey, bIV);
        byte[] encrypt = des.encrypt(content);

        System.out.println("token = " + Base64.encode(encrypt));
        return Base64.encode(encrypt);
    }

    public static void main(String[] args) {
        String timeStamp = "2019-07-22 11:44"; //DateUtil.format(DateUtil.date(), "yyyy-MM-dd hh:mm");
        String combStr = siteId + timeStamp + appUrl;
        PassportUtil.getAuthenticator(combStr);
    }
}
